package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

//权限管理模块
@Service(interfaceClass = PermissionsService.class)
@Transactional
public class PermissionsServiceImpl implements PermissionsService {

    @Autowired
    private PermissionDao permissionDao;

    //分页查询权限
    @Override
    public PageResult findPage(String queryString, Integer pageSize, Integer currentPage) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> page = permissionDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //查询所有权限
    @Override
    public List<Permission> queryAllPermissions() {
        return permissionDao.queryAllPermissions();
    }

    //添加权限
    @Override
    public void addPermissions(Permission permission) {
        permissionDao.addPermissions(permission);
    }

    //回显权限
    @Override
    public Permission findById(Integer permissionsId) {
        return permissionDao.findById(permissionsId);
    }

    //编辑权限
    @Override
    public int editPermissions(Permission permission) {
        return permissionDao.editPermissions(permission);
    }

    //删除权限
    @Override
    public void deleteByPermissionsId(Integer id) {
        //根据权限id删除中间关系表的数据
        permissionDao.deleteByRolePermissionsId(id);
        //根据权限id删除权限
        permissionDao.deleteByPermissionsId(id);
    }

    //查看此权限是否关联角色
    @Override
    public int findPermissionsQueryRole(Integer id) {
        return permissionDao.findPermissionsQueryRole(id);
    }

    //根据权限名和权限关键字查询权限
    @Override
    public Permission queryPermission(String name, String keyword) {
        return permissionDao.queryPermission(name,keyword);
    }

    //根据权限id查询角色
    @Override
    public List<Map<String,String>> handleQueryRole(Integer permissionsId) {
        return permissionDao.handleQueryRole(permissionsId);
    }

    //绑定角色
    @Override
    public void handleEditUser(Map<String, Integer> map, Integer[] roleIds) {
        Integer permissionId = map.get("permissionId");
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                permissionDao.handleEditUser(roleId,permissionId);
            }
        }
    }
}
